#include <task_dispatcher.h>

#include <iostream>

class AddTask : public dispatcher::Task
{
public:
    AddTask(int val0, int val1) : val0_(val0), val1_(val1), sum_(0)
    {
    }

    void Run()
    {
        std::cout << "[ \e[1;35mDEBUG\e[0m ]: AddTask::Run(), thread_id=" << std::this_thread::get_id() << std::endl;
        sum_ = val0_ + val1_;
        std::this_thread::sleep_for(std::chrono::seconds(3));
    }

    int GetResult()
    {
        return sum_;
    }

private:
    int val0_;
    int val1_;
    int sum_;
};

int main(int argc, char* argv[])
{
    std::cout << "Hello, TaskDispatcher." << std::endl;
    dispatcher::TaskDispatcher dispatcher(10);
    std::shared_ptr<AddTask>   task0(std::make_shared<AddTask>(100, 111));
    std::shared_ptr<AddTask>   task1(std::make_shared<AddTask>(874, 111));
    dispatcher.AddTask(task0);
    dispatcher.AddTask(task1);

    task0->Wait();
    task1->Wait();

    std::cout << "[ \e[1;36mOK\e[0m ] AddTask::result = " << task0->GetResult() << std::endl;
    std::cout << "[ \e[1;36mOK\e[0m ] AddTask::result = " << task1->GetResult() << std::endl;

    std::shared_ptr<AddTask> task2(std::make_shared<AddTask>(1, 1));
    std::shared_ptr<AddTask> task3(std::make_shared<AddTask>(110, 1));
    std::shared_ptr<AddTask> task4(std::make_shared<AddTask>(996, 1));
    dispatcher.AddTask(task2);
    dispatcher.AddTask(task3);
    dispatcher.AddTask(task4);
    task2->Wait();
    task3->Wait();
    task4->Wait();
    std::cout << "[ \e[1;36mOK\e[0m ] AddTask::result = " << task2->GetResult() << std::endl;
    std::cout << "[ \e[1;36mOK\e[0m ] AddTask::result = " << task3->GetResult() << std::endl;
    std::cout << "[ \e[1;36mOK\e[0m ] AddTask::result = " << task4->GetResult() << std::endl;
    return 0;
}